package com.lzhpo.core.service;

import com.google.common.collect.Lists;
import com.lzhpo.core.domain.CommonVo;
import com.lzhpo.core.domain.PrizeInfoEntity;
import com.lzhpo.core.domain.concord.SpanDataVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author <a href="lijing1@wxchina.com@wxchina.com">Lijin</a>
 * @Description TODO
 * @Date 2020/1/16 13:54
 * @Version 1.0
 **/
@Service
public class CommonService {


    public void copyCommonVo(CommonVo src, CommonVo target) {
        target.setId(src.getId());
        target.setTermNo(src.getTermNo());
        target.setPrizeNo01(src.getPrizeNo01());
        target.setPrizeNo02(src.getPrizeNo02());
        target.setPrizeNo03(src.getPrizeNo03());
    }

    /**
     *  统一返回底部统计三行
     * @param bigList 一行的数据封装成list
     * @param length 数组的宽度
     * @return
     */
    public List<String[]> getCommonBottomStatics(List<List<String>> bigList , int length) {

        List<String[]> statics = Lists.newArrayList();
        Integer[] arrCount = new Integer[length];
        String[] arrContinue = new String[length];
        String[] arrMiss = new String[length];
        //每一行
        for (int i = 0; i < bigList.size(); i++) {
            List<String> temp = bigList.get(i);//21
            //每一行的各列
            for (int j = 0; j < temp.size(); j++) {
                String tempStr = temp.get(j);
                //计算出现次数
                if (tempStr.startsWith("ok")) {
                    if (arrCount[j] == null) {
                        arrCount[j] = 1;
                    } else {
                        arrCount[j] = 1 + arrCount[j];
                    }
                } else {
                    if (arrCount[j] == null) {
                        arrCount[j] = 0;
                    }

                }
                //最大遗漏
                if (tempStr.startsWith("no")) {
                    if (arrMiss[j] == null) {
                        arrMiss[j] = "1,0";
                    } else {
                        int value = Integer.valueOf(arrMiss[j].split(",")[0]) + 1;
                        int valuePre = Integer.valueOf(arrMiss[j].split(",")[1]);
                        arrMiss[j] = value + "," + valuePre;
                    }

                } else {
                    if (arrMiss[j] == null) {
                        arrMiss[j] = "0,0";
                        //断啦。将值保存
                    } else {
                        int value = Integer.valueOf(arrMiss[j].split(",")[0]);
                        int valuePre = Integer.valueOf(arrMiss[j].split(",")[1]);
                        int maxValueTemp = Math.max(value, valuePre);
                        arrMiss[j] = "0," + maxValueTemp;
                    }
                    //arrMiss[j]=0;
                }
                //最大连出 ok。  0,0   以累计的放后面，目前计算的放前面
                if (tempStr.startsWith("ok")) {

                    if (arrContinue[j] == null) {
                        arrContinue[j] = "1,0";
                    } else {
                        int value = Integer.valueOf(arrContinue[j].split(",")[0]) + 1;
                        int valuePre = Integer.valueOf(arrContinue[j].split(",")[1]);
                        arrContinue[j] = value + "," + valuePre;
                    }
                } else {
                    if (arrContinue[j] == null) {
                        arrContinue[j] = "0,0";
                        //断啦。将值保存
                    } else {
                        int value = Integer.valueOf(arrContinue[j].split(",")[0]);
                        int valuePre = Integer.valueOf(arrContinue[j].split(",")[1]);
                        int maxValueTemp = Math.max(value, valuePre);
                        arrContinue[j] = "0," + maxValueTemp;
                    }
                }
            }
        }
        List<Integer> arrContinueIntList =
                Lists.newArrayList(arrContinue).stream()
                        .map(s -> Math.max(
                                Integer.valueOf(s.split(",")[0]), Integer.valueOf(s.split(",")[1])
                        )).collect(Collectors.toList());
        List<Integer> arrMissIntList =
                Lists.newArrayList(arrMiss).stream()
                        .map(s -> Math.max(
                                Integer.valueOf(s.split(",")[0]), Integer.valueOf(s.split(",")[1])
                        )).collect(Collectors.toList());

        Integer[] arrContinueIntArr = transListToArr(arrContinueIntList);
        Integer[] arrMissIntArr = transListToArr(arrMissIntList);
        String[] arrCountStr = new String[arrCount.length];
        for (int i = 0; i < arrCountStr.length; i++) {
            arrCountStr[i] = String.valueOf(arrCount[i]);
        }
        arrCountStr = StringUtils.addStringToArray(arrCountStr, "出现次数");
        statics.add(arrCountStr);//累计
        String[] arrContinueIntArrStr = new String[arrContinueIntArr.length];
        for (int i = 0; i < arrContinueIntArr.length; i++) {
            arrContinueIntArrStr[i] = String.valueOf(arrContinueIntArr[i]);
        }
        arrContinueIntArrStr = StringUtils.addStringToArray(arrContinueIntArrStr, "最大连出");
        statics.add(arrContinueIntArrStr);//最大连出
        String[] arrMissStr = new String[arrMissIntArr.length];
        for (int i = 0; i < arrMissIntArr.length; i++) {
            arrMissStr[i] = String.valueOf(arrMissIntArr[i]);
        }
        arrMissStr = StringUtils.addStringToArray(arrMissStr, "最大遗漏");
        statics.add(arrMissStr);//最大遗漏
        return statics;
    }


    /**
     * 将集合转为数组
     *
     * @param list
     * @return
     */
    public Integer[] transListToArr(List<Integer> list) {

       /* Integer[] arr = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }*/
        return list.stream().toArray(Integer[]::new);
    }

}
